<script setup lang="ts">
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'
import { getKnowledgePage } from '@/services/consult'
import { Knowledge, KnowledgeList, KnowledgePage, KnowledgeType } from '@/types/consult'      

const props = defineProps<{
  type: KnowledgeType
}>()


const loading = ref(false)
const finished = ref(false)

const list = ref<KnowledgeList>([])
const params = ref<KnowledgeParams>({
    type: props.type,
    current: 1,
    pageSize: 10
  })




// 获取数据
const onLoad = async ()=>{
  const  res = await getKnowledgePage()
  list.value.push(...res.data.rows)  // []  .length

  if(params.value.current >= res.data.pageTotal){
    finished.value = true
  }else{
    params.value.current++
  }

  loading.value = false
}



// const onLoad = () => {
//   // 加载数据
//   console.log('loading')
//   // 模拟加载更多
//   setTimeout(() => {
//     const data = [1, 2, 3, 4, 5]
//     list.value.push(...data)
//     // 模拟加载完毕
//     if (list.value.length > 20) {
//       finished.value = true
//     }
//     loading.value = false
//   }, 1000)
// }
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
    <!-- // list -->
      <knowledge-card v-for="item in list" :key="item.id" :item="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>